<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once ('./application/controllers/authenticate.php');
class Translator extends Authenticate {
	
	const PAGE_RANGE = 5;
	const ITEM_PER_PAGE = 20;
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('group_translator_model');
		$this->load->library('html/control/fx_control');
		$this->load->library('html/control/fx_filter');
		$this->load->library('html/control/fx_sort');
		$this->group_translator_model->addAttributeOrder('id', 'desc');
		$this->load->library('upload');
		// Load Translator Model
		$this->loadCurrentTranslator();
	}

	public function loadCurrentTranslator() 
	{
		try {
			// Load Translator Model
			$id = $this->input->getParam('id');
			if ($id) {
				$this->group_translator_model->loadByPK($id);
				$this->st_registry->register('admin/group/translator/id', $id);
			} else {
				if ($id = $this->st_registry->registry('admin/group/translator/id'))
				{
					$this->group_translator_model->loadByPK($id);
				}
			}
			return $this->group_translator_model;
		} catch (Exception $ex) {
			return null;
		}
	}
	public function index() 
	{	
		try {
			//Unload Current Translator
			$this->st_registry->clearRegistry('admin/group/translator/id');
			//Get Data
			$data = $this->_initGridView();
			//Transfer To View
			$this->load->view('group/translator', $data);
		} catch (Exception $ex) {
			echo $ex->getMessage();
		}
	}
	
	public function edit() 
	{
		try {
			$data['model'] = $this->group_translator_model;
			$this->load->view('group/translator/edit', $data);	
		} catch (Exception $ex) {
			echo $ex->getMessage();
		}
	}
	
	protected function _initGridView()
	{
		$this->group_translator_model->resetFilterList();
		// Get Paginator Params
		$page = $this->input->getParam('page');
		$recordPerPage = $this->input->getParam('recordPerPage');
		if (!$recordPerPage) {
			$recordPerPage = self::ITEM_PER_PAGE;
		}
		// Get Filter Params	
		$filters = $this->input->post('translatorFilter');
		// Filter
		$this->fx_filter->filter($filters, $this->group_translator_model);
		// Get Order Params
		$order = $this->input->post('translatorOrder');
		// Sort
		$this->fx_sort->sort($order, $this->group_translator_model);
		// Get total found records
		$found = $this->group_translator_model->countTotal();
		// Paginate
		$this->load->library("tree/paginator/engine/st_paginator_collection");
		$this->load->library("tree/paginator/st_paginator");
		$this->st_paginator_collection->setModel($this->group_translator_model);
		$this->st_paginator->setConfig(array(
			'engine' => $this->st_paginator_collection,
			'pageRange' => self::PAGE_RANGE,
			'itemCountPerPage' => $recordPerPage,
			'currentPageNumber' => $page  
		));		
		// Paginate Collection
		$collection = $this->st_paginator->getItemsByPage($page);
		if (empty($collection)) {
			$collection = array();
		}
		$data = array();
		$data['translator_selected'] = $this->input->post('translator_selected');
		$data['found'] = $found;
		$data['translator_ids'] = $this->input->post('translator_ids');
		$data['translator_ids_array'] = explode(',', $this->input->post('translator_ids'));
		
		/*END RADIO SELECTED*/
		//echo $data['translator_ids'];
		$data['model'] = $this->group_translator_model;
		$data['sorter'] = $this->fx_sort;
		$data['filters'] = $this->fx_filter;
		$data['paginator'] = $this->st_paginator;
		$data['collection'] = $collection;
		return $data;
	}
	 
	public function save() 
	{
		try {
			//print_r ($this->input->post('translator'));
			// Load Translator Model
			$id = $this->input->post('id');
			if ($id) {
				$this->group_translator_model->loadByPK($id);
			}
			$data = $this->input->post('translator');
			if (is_array($data) && count($data) > 0) {
				$this->group_translator_model->setEntireData($data);
				//Upload Thumb Image
				$config['upload_path'] = './media/translator/icons/';
				$config['allowed_types'] = 'gif|jpg|png|bmp';
				$config['max_size']	= '300';
				$this->upload->initialize($config);
				$this->upload->do_upload('translator_icon');
				$dataUpload = $this->upload->data();
				$iconPath = $config['upload_path'].$dataUpload['file_name'];
				if (strlen($dataUpload['file_name']) > 0) {
					$this->group_translator_model->setIcon($iconPath);
				}
				$this->group_translator_model->save();
				redirect(site_url("admin/group/translator/edit/id/".$this->group_translator_model->getId(), true));
			}
			redirect(site_url("admin/group/translator/index", true));
		} catch (Exception $ex) {
			echo $ex->getMessage();
		}
	}
	
	public function delete()
	{
		try {
			if ($this->group_translator_model->isLoaded()) {
				$name = $this->group_translator_model->getName();
				$this->group_translator_model->delete();
				$this->st_registry->register('admin/group/translator/message', "Delete translator $name successfully");
				redirect(site_url("admin/group/translator/index", true));
			}
			else {
				$this->st_registry->register('admin/group/translator/message', "Cannot delete translator");
				redirect(site_url("admin/group/translator/index", true));
			}
		} catch (Exception $ex) {
			$this->st_registry->register('admin/group/translator/message', "Error when deleting translator");
			redirect(site_url("admin/group/translator/index", true));
		}
	}
	
	public function massDelete()
	{
		try {
			$ids = $this->input->getMassValues('translator_ids');
			if ($ids) {
				$this->group_translator_model->query("delete from group_translator where id in ($ids)");
			}
			$countItem = count(explode(',', $ids));
			$this->st_registry->register('admin/group/translator/message', "Delete [$countItem] items successfully");
			redirect(site_url("admin/group/translator/index", true));
		} catch (Exception $ex) {
			$this->st_registry->register('admin/cms/translator/message', $e->getMessage());
			redirect(site_url("admin/cms/translator/index", true));
		}
	}
}